#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2023/12/15 21:06
# @Author  : FuKai ZHANG
# @File    : send_email.py
# @Description :
import smtplib
from email.mime.text import MIMEText
from config.global_config import config
from message_template import Template
from utils.other_tools.allure_report_data import AllureFileClean


class SendEmail:
    """ 发送邮箱 """

    @classmethod
    def send_mail(cls, user_list: list, sub, content: str) -> None:
        """

        @param user_list: 发件人邮箱
        @param sub:
        @param content: 发送内容
        @return:
        """
        user = "FuKaiZHang" + "<" + config.email.send_user + ">"

        message = MIMEText(content, _subtype='plain', _charset='utf-8')
        # 邮件主题
        message['Subject'] = sub
        # 收件人
        message['To'] = ";".join(user_list)  # 分号连接
        server = smtplib.SMTP()
        # 连接服务器
        server.connect(config.email.email_host)
        # 登录服务器
        server.login(config.email.send_user, config.email.stamp_key)
        # 发送邮件
        server.sendmail(user, user_list, message.as_string())
        # 关闭连接
        server.close()

    def send_main(self) -> None:
        """
        发送邮件
        :return:
        """
        email = config.email.send_list
        user_list = email.split(',')  # 多个邮箱发送，yaml文件中直接添加

        sub = config.project_name + f"Version:{config.project_version}" + "web自动化测试结果"
        # 定义邮件模板，根据需要自行修改
        content= Template(AllureFileClean().get_case_count()).get_template
        self.send_mail(user_list, sub, content)


if __name__ == '__main__':
    SendEmail().send_main()
